<template>
  <page-header-wrapper>
    <page-tab-list>
      <div class="table-page-search-box">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :md="8" :sm="24">
              <a-form-item label="">
                <a-input-group compact>
                  <a-select v-model="queryParam.search_type" style="width: 80px">
                    <a-select-option value="1"> 订单号 </a-select-option>
                    <a-select-option value="2"> 名称 </a-select-option>
                  </a-select>
                  <a-input v-model="queryParam.keyword" style="width:calc(100% - 80px)" placeholder="请输入关键词" />
                </a-input-group>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="支付状态">
                <a-select placeholder="请选择" v-model="queryParam.status" default-value="1">
                  <a-select-option value="-1">全部</a-select-option>
                  <a-select-option value="0">未分账</a-select-option>
                  <a-select-option value="1">已分账</a-select-option>
                  <a-select-option value="2">分账失败</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="订单时间">
                <a-config-provider :locale="locale">
                  <a-range-picker
                    format="YYYY-MM-DD HH:mm:ss"
                    style="width: 100%"
                    @change="onChange"
                    @calendarChange="calendarChange"
                    :ranges="ranges"
                  />
                </a-config-provider>
              </a-form-item>
            </a-col>
          </a-row>
          <a-row :gutter="24">
            <a-col :md="8" :sm="24">
              <a-form-item label="订单来源">
                <a-select v-model="queryParam.from" placeholder="请选择">
                  <a-select-option value="-1">全部</a-select-option>
                  <a-select-option value="0">店铺分销</a-select-option>
                  <a-select-option value="1">平台分销</a-select-option>
                  <a-select-option value="2">平台推荐</a-select-option>
                  <a-select-option value="3">服务商分账</a-select-option>
                  <a-select-option value="4">商城间分销</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="支付状态">
                <a-button style="margin-right: 8px" @click="resetQueryParams">重置</a-button>
                <a-button class="search-item" type="primary" @click="refresh">查询</a-button>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </page-tab-list>
    <a-card :bordered="false">
      <s-table rowKey="id" size="default" :columns="columns" :data="loadData" ref="table">
        <span slot="status" slot-scope="text, record">
          <template v-if="text == 0"> 未分账 </template>
          <template v-else-if="text == 1"> 分账成功 </template>
          <template v-else-if="text == 2">
            分账失败：<span>{{ record.err_code_des }}</span>
          </template>
        </span>
      </s-table>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import zhCN from 'ant-design-vue/lib/locale-provider/zh_CN'
import moment from 'moment'
import pick from 'lodash.pick'
import { STable } from '@/components'
import { getProfitList } from '@/api/saas/profit'

const STATUS = {
  1: 'PERSONAL_OPENID',
  2: 'MERCHANT_ID',
  3: 'PERSONAL_SUB_OPENID',
}

const columns = [
  {
    title: 'ID',
    dataIndex: 'id',
  },
  {
    title: '订单号',
    dataIndex: 'order_no',
  },
  {
    title: '账号',
    dataIndex: 'account',
  },
  {
    title: '分账描述',
    dataIndex: 'description',
  },
  {
    title: '账户名称',
    dataIndex: 'name',
  },
  {
    title: '比例',
    dataIndex: 'rate',
  },
  {
    title: '金额',
    dataIndex: 'amount',
  },
  {
    title: '状态',
    dataIndex: 'is_pay',
    scopedSlots: { customRender: 'status' },
  },
  {
    title: '来源',
    dataIndex: 'remark',
  },
  {
    title: '创建时间',
    dataIndex: 'created_at',
  },
]

export default {
  name: 'ProfitSharingRecord',
  components: {
    STable,
  },
  data() {
    return {
      selectDate: {},
      locale: zhCN,
      ranges: {
        一月内: () => [moment().startOf('day').subtract(1, 'month'), moment()],
        昨天: () => [moment().startOf('day').subtract(1, 'days'), moment().endOf('day').subtract(1, 'days')],
        最近一周: () => [moment().startOf('day').subtract(1, 'weeks'), moment()],
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 3 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 20 },
      },
      form: this.$form.createForm(this),
      // 表头
      columns,
      // 权限树
      permissionTree: [],
      modalLoading: false,
      defaultCheckedKeys: [],
      // 查询条件参数
      queryParam: {
        search_type: '1',
        status: '-1',
        dateStart: null,
        dateEnd: null,
        from: '-1',
        type: '2',
      },
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        Object.assign(parameter, this.queryParam)
        return getProfitList(parameter).then((res) => {
          return res.data
        })
      },
    }
  },
  filters: {},
  created() {},
  methods: {
    refresh(row) {
      this.$nextTick(() => {
        this.$refs.table.refresh(true)
      })
    },
    moment,
    calendarChange(date, dateString) {
      this.selectDate = date[0]
    },
    onChange(date, dateString) {
      this.queryParam.dateStart = dateString[0]
      this.queryParam.dateEnd = dateString[1]
    },
    resetQueryParams() {
      Object.assign(this.queryParam, {
        search_type: '1',
        status: '-1',
        dateStart: null,
        dateEnd: null,
        from: '-1',
        keyword: null,
        type: '2',
      })
      this.$refs.table.refresh(true)
    },
  },
}
</script>

<style lang="less" scoped>
.permission-form {
  /deep/ .permission-group {
    margin-top: 0;
    margin-bottom: 0;
  }
}
.class_p {
  height: 10px;
  font-size: 12px;
  color: red;
  padding: 6px;
}
</style>
